<?php
ini_set("error_reporting","E_ALL & ~E_NOTICE");
header('Content-type: application/json');
$jsoncallback = htmlspecialchars($_REQUEST ['jsoncallback']);//获取回调函数名

$url=$_GET["url"];$url = preg_replace('# #','',$url);
$title=$_GET["title"];$title = preg_replace('# #','',$title);
$description=$_GET["description"];$description = preg_replace('# #','',$description);
$keywords=$_GET["keywords"];$keywords = preg_replace('# #','',$keywords);
$type=$_GET["type"];$type = preg_replace('# #','',$type);
$state=$_GET["state"];$state = preg_replace('# #','',$state);
$token=$_GET["token"];$token = preg_replace('# #','',$token);

if ($state != "显示") {
    $state = "隐藏";
}

if (!$url or !$title or !$description or !$keywords or !$type) {
    $data['status']="NO";
    $data['message']="数据验证失败[关键字不能为空数值]，请您检查后重试！";
    echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
    die();
}

if(preg_match('/^[ _:：,，.。…\/、~`＠＃￥％＆×＋｜｛｝＝－＊＾＄～｀!@#$%^&*()\+-—=（）！￥{}【】\[\]\|\"\'’‘“”；;《》<>\?\？\·]+$/u', $title)>0){
    $data['status']="NO";
    $data['message']="网站名称数据不合法[包含特殊字符],请您检查后重试！";
    echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
    die();
}

if ($token != "Jmwpower") {
    $data['status']="NO";
    $data['message']="数据验证失败[Token 口令不正确]，请您检查后重试！";
    echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
    die();
}

$servername = "localhost";
$username = "username";//需要修改为你的 数据
$password = "password";
$dbname = "dbname";

$con = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){// 检测连接-失败回传钉钉信息
    mysqli_close($con);//关闭数据库
    $data['status']="NO";
    $data['message']="链接数据库失败，请稍候刷新重试！";
    echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
}else{//读取记录数据
    $sql = "SELECT * FROM url WHERE url='$url' or `title`='$title'";//查询历史凭证数据
    $result = $con->query($sql);
    if ($result->num_rows > 0) {//已存在数据，更新维护
        while($row = mysqli_fetch_assoc($result)) {//输出数据
            $url_id=$row["id"];
        }
        if(mysqli_query($con,"UPDATE `url` SET `url`='$url',`title`='$title',`description`='$description',`keywords`='$keywords',`type`='$type',`state`='$state' WHERE `id`='$url_id'")==true){//更新数据成功
            $data['status']="YES";
            $data['message']="YES！！！维护更新数据成功！";
            $data['data'][0]["id"] = $url_id;
            $data['data'][0]["title"] = $title;
            $data['data'][0]["url"] = $url;
            $data['data'][0]["description"] = $description;
            $data['data'][0]["keywords"] = $keywords;
            $data['data'][0]["type"] = $type;
            $data['data'][0]["state"] = $state;
            echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
            mysqli_close($con);//关闭数据库
        }else{
            $data['status']="NO";
            $data['message']="NO！维护更新数据失败！请稍候刷新重试！";
            echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
            mysqli_close($con);//关闭数据库
        }
    } else {
        $date = date("YmdHis");
        if(mysqli_query($con,"INSERT INTO `url`(`id`, `url`, `title`,`description`, `keywords`,  `type`, `state`) VALUES ('$date','$url','$title','$description','$keywords','$type','$state')")==true){//上载数据成功
            $data['status']="YES";
            $data['message']="YES！！！添加数据成功！";
            $data['data'][0]["id"] = $date;
            $data['data'][0]["title"] = $title;
            $data['data'][0]["url"] = $url;
            $data['data'][0]["description"] = $description;
            $data['data'][0]["keywords"] = $keywords;
            $data['data'][0]["type"] = $type;
            $data['data'][0]["state"] = $state;
            echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
            mysqli_close($con);//关闭数据库
        }else{
            $data['status']="NO";
            $data['message']="NO！添加数据失败！请稍候刷新重试！";
            echo $jsoncallback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
            mysqli_close($con);//关闭数据库
        }
    }
}
?>